package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.item.config.ThreadPoolConfig;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;


@RestController
@RequestMapping("api/item")
public class ItemApiController {



    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("/getItem/{skuId}")
    @ApiOperation(value = "数据汇总方法")
    public Map getItem(@ApiParam(value = "库存ID") @PathVariable Long skuId){
        Map result = new HashMap();
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //    1、根据skuId查询sku_info  根据skuId查询sku_image  /inner：内部资源
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo",skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryView1 = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //3、根据category3Id查询一二三级分类
            BaseCategoryView categoryView
                    = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", categoryView);
        },threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrList1 = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //4、根据spuId查询spu_sale_attr_value(被选中项）  商品ID spu  库存ID
            List<SpuSaleAttr> spuSaleAttrList
                    = productFeignClient.getSpuSaleAttrValue(skuInfo.getSpuId(), skuId);
            result.put("spuSaleAttrList", spuSaleAttrList);
        },threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJson1 = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //5、skuId与销售属性组合
            Map valuesSkuJson = productFeignClient.spuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(valuesSkuJson));
        },threadPoolExecutor);

        CompletableFuture<Void> priceProductFeignClient = CompletableFuture.runAsync(() -> {
            //2:查询实时价格   优化的时候 不要放进缓存中
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        //远程调用搜索服务的加分接口
        CompletableFuture.runAsync(()->{
            listFeignClient.hotScore(skuId,500L);
        },threadPoolExecutor);

        //多任务组合
        CompletableFuture.allOf(priceProductFeignClient,
                                categoryView1,
                                spuSaleAttrList1,
                                valuesSkuJson1).join();
        return result;
    }
}
